//
//  SineWaveModule.m
//  TengineTwo
//
//  Created by StandardUser on 10/02/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
//  updates an entities y position based on a sine wave
//  x position is wrapped linearly
//

#import "GhostModule.h"
#import "CGPointExtension.h"
#import "EntityTypes.h"
#import "EntityModel.h"
#import "ccMacros.h"

@interface GhostModule()

@property(readwrite, nonatomic)float nextT; //point in the future that updates have been executed up to
@property(readwrite, nonatomic)float prevT;
@property(readwrite, nonatomic)float freq;

@end

@implementation GhostModule

@synthesize nextT;
@synthesize prevT;
@synthesize freq;

- (id)init {
    self = [super init];
    if (self) {
        //freq = M_PI / (float)(arc4random()%5);
        //NSLog(@"freq %f",freq);
    }
    return self;
}

-(void)update:(EntityModel *)e FromT:(float)t
{
    CGPoint position = ccp(0, 0);
    
    position.x = (t - e.tOffset) * e.speed;
    //if(position.x > 480)    position.x = 0;
    //if(position.x < 0)      position.x = 480;
    
    float amp = 100;
    freq = M_PI / 5; //hz
    position.y = sinf((t - e.tOffset) * freq) * amp;
    
    e.position = ccpAdd(position, e.origin);
    
    if(t > nextT) nextT = t;
    
    float tDelta = t - prevT;
    prevT = t;
    if(tDelta < 0 || t < nextT) return; //only spew bullets moving forward and don't spew bullets for any T where we have done so before
    
    if(e.controller)
    {
        id<EntityModelControllerDelegate> controller = e.controller;
        if(arc4random() % 100 > 90)
        {
            id<Entity>bullet = [controller addEntityOfType:BULLET];
            bullet.rotation = CC_DEGREES_TO_RADIANS(arc4random() % 360);
            bullet.origin = e.position;
        }
    }
}

@end
